<template>
  <div class="setting-queue">
    <div class="queue-header">
      <el-button type="primary" size="small" @click="dialogFormVisible = true">添加队列</el-button>
    </div>
    <el-table :data="queuelist" style="width: 100%">
      <el-table-column prop="pool" label="队列名称"> </el-table-column>
      <el-table-column prop="description" label="描述"> </el-table-column>
      <el-table-column prop="slots" label="队列大小"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            circle
            @click="edit(scope.row)"
          ></el-button>
          <el-popconfirm
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @onConfirm="handleDelete(scope.row)"
          >
            <el-button
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right; margin-top: 8px">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageParams.page"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="totalCount"
      >
      </el-pagination>
    </div>
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="!this.form.id ? '添加队列' : '编辑队列'"
      width="400px"
    >
      <el-form ref="form" :model="form" label-width="90px" label-position="right" :rules="rules">
        <el-form-item label="队列名称:" prop="pool">
          <el-input v-model="form.pool"></el-input>
        </el-form-item>
        <el-form-item label="队列大小:" prop="slots">
          <el-input v-model="form.slots" type="number" min="0"></el-input>
        </el-form-item>
        <el-form-item label="描述:" prop="desc">
          <el-input type="textarea" :autosize="{ minRows: 2 }" v-model="form.desc"> </el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addWarn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "manageWarn",
  data() {
    return {
      pageParams: {
        page: 1
      },
      totalCount: 0,
      queuelist: [],
      dialogFormVisible: false,
      form: {
        pool: "",
        slots: "",
        desc: ""
      },
      rules: {
        pool: [{ required: true, message: "请输入队列名称", trigger: "blur" }],
        slots: [{ required: true, message: "请输入队列大小", trigger: "blur" }]
      }
    };
  },

  methods: {
    handleCurrentChange(val) {
      this.pageParams.page = val;
      this.initData();
    },
    initData() {
      this.$http.queue.getQueueList({ ...this.pageParams }).then((res) => {
        this.queuelist = res.items;
        this.totalCount = res.total;
      });
    },
    edit(row) {
      this.$http.queue.getQueueById({ id: row.id }).then((res) => {
        this.form.id = res.id;
        this.form.desc = res.description;
        this.form.pool = res.pool;
        this.form.slots = res.slots;
        this.dialogFormVisible = true;
      });
    },
    handleDelete(row) {
      this.$http.queue.delQueue({ id: row.id }).then((res) => {
        this.$message({
          message: "删除成功",
          type: "success"
        });
        this.initData();
      });
    },
    addWarn() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (!this.form.id) {
            this.$http.queue.addQueue(this.form).then((res) => {
              this.$message({
                message: "新增成功",
                type: "success"
              });
              this.initData();
              this.dialogFormVisible = false;
            });
          } else {
            this.$http.queue.editQueue(this.form).then((res) => {
              this.$message({
                message: "修改成功",
                type: "success"
              });
              this.initData();
              this.dialogFormVisible = false;
            });
          }
        } else {
          return false;
        }
      });
    }
  },
  watch: {
    dialogFormVisible(val) {
      if (!val) {
        this.$refs.form.resetFields();
        this.form = {
          pool: "",
          slots: "",
          desc: ""
        };
      }
    }
  },
  created() {
    this.initData();
  }
};
</script>
<style lang="scss" scoped>
.setting-queue {
  .queue-header {
    float: right;
    margin-bottom: 8px;
  }
}
/deep/.el-table__header-wrapper .el-table__header th {
  background-color: #f8f8f9;
  color: #515a6e;
  font-size: 14px;
  font-weight: 500;
}
</style>

